package com.bosssoft.ecds.controller;

/**
 * @author 张东海
 * @date 2020/8/10
 * @description 财政端申请具体票据信息Controller，提供了基本的增删改查
 */

import cn.hutool.core.bean.BeanUtil;
import com.bosssoft.ecds.common.response.CommonCode;
import com.bosssoft.ecds.common.response.QueryResponseResult;
import com.bosssoft.ecds.common.response.ResponseResult;
import com.bosssoft.ecds.entity.po.FbeStockFinanceapplyItemPO;
import com.bosssoft.ecds.entity.vo.FbeStockFinanceapplyItemVO;
import com.bosssoft.ecds.service.impl.FbeStockFinanceapplyItemServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class FbeStockFinanceapplyItemController {

    @Resource
    FbeStockFinanceapplyItemServiceImpl fbeStockFinanceapplyItemService;

    @GetMapping("fbeStockFinanceapplyItem")
    public QueryResponseResult getById(BigInteger id){
        FbeStockFinanceapplyItemPO financeapplyItemPo = fbeStockFinanceapplyItemService.getByPrimaryKey(id);
        return new QueryResponseResult(CommonCode.SUCCESS,financeapplyItemPo);
    }

    @GetMapping("fbeStockFinanceapplyItem/getAll")
    public QueryResponseResult getAll(){
        List<FbeStockFinanceapplyItemPO> pos = fbeStockFinanceapplyItemService.getAll();
        List<FbeStockFinanceapplyItemVO> itemVOList = pos.stream()
                .map(po -> {
                    FbeStockFinanceapplyItemVO vo = new FbeStockFinanceapplyItemVO();
                    BeanUtil.copyProperties(po, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        return new QueryResponseResult(CommonCode.SUCCESS,itemVOList);
    }

    @PostMapping("fbeStockFinanceapplyItem")
    public ResponseResult create(FbeStockFinanceapplyItemPO fbeStockFinanceapplyItem){
        int insert = fbeStockFinanceapplyItemService.insert(fbeStockFinanceapplyItem);
        if(insert > 0){
            return ResponseResult.SUCCESS();
        }else {
            return ResponseResult.FAIL();
        }

    }

    @PutMapping("fbeStockFinanceapplyItem")
    public ResponseResult update(FbeStockFinanceapplyItemPO fbeStockFinanceapplyItem){
        fbeStockFinanceapplyItemService.update(fbeStockFinanceapplyItem);
        return ResponseResult.SUCCESS();
    }

    @DeleteMapping("fbeStockFinanceapplyItem")
    public ResponseResult delete(BigInteger id){
        fbeStockFinanceapplyItemService.delete(id);
        return ResponseResult.SUCCESS();
    }

}
